package com.lin.reggie.controller;

import com.lin.reggie.common.R;
import com.lin.reggie.dto.DishDto;
import com.lin.reggie.entity.Page;
import com.lin.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")

public class DishController {

    @Autowired(required = false)
    private DishService dishService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session ){
        //获取登陆人id
        Long id = (Long) session.getAttribute("employee");
        //完善修改人，创建人
        dishDto.setUpdateUser(id);
        dishDto.setCreateUser(id);
        //调用业务层把数据添加到数据库
        dishService.save2(dishDto);
        //返回数据
        return R.success("添加成功");
    }

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>>findPage(Integer page ,Integer pageSize ,String name){
        Page<DishDto>pageObj=dishService.findByPage2(page,pageSize,name);
        return R.success(pageObj);
    }
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable("id") Long id) {
        DishDto byId = dishService.findById(id);
        return R.success(byId);
    }
    @PutMapping
    public R<String>update(@RequestBody DishDto dishDto,HttpSession session){
        //1.获取当前登录者
        Long empId = (Long) session.getAttribute("employee");

        //设置修改人
        dishDto.setUpdateUser(empId);

        //调用业务层进行修改
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>>findByDishCategoryId( Long categoryId,Integer status){
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,status);
        return R.success(dishList);
    }
}
